import QtQuick
import QtQuick.Window
import QtQuick.Controls
import QtQuick.Dialogs
import QtQuick.Layouts

Window {
    width : 600
    height : 600
    minimumWidth: 600
//    maximumWidth: 600
    minimumHeight: 600
//    maximumHeight: 600
    visible : true
    title : qsTr("STM32 IAP")
    color : "white"
    Text {
        anchors.top: parent.top
        anchors.right: parent.right
        anchors.topMargin: 10
        anchors.rightMargin: 10
        text: "V2_" + Qt.formatDateTime(new Date(), "yyyyMMdd_HHmm")
    }

    Grid {
        id : set_mode
        anchors.top : parent.top
        anchors.left : parent.left
        anchors.topMargin : 10
        anchors.leftMargin : 15
        layoutDirection : Grid.LeftToRight
        verticalItemAlignment: Grid.AlignVCenter
        spacing : 10
        rows : 1
        columns : 3
        Text {text: qsTr("模式设置:")}
        ColumnLayout {
            RadioButton {
                id : rbtn_send
                checked: true
                text: qsTr("写入固件")
                onClicked: {
                    txt_filename.text = qsTr("请输入要写入的固件地址");
                }
            }
            RadioButton {
                id : rbtn_recv
                text: qsTr("读取固件")
                onClicked: {
                    txt_filename.text = qsTr("请输入要保存的固件地址");
                }
            }
        }
        ColumnLayout {
            RadioButton {
                id : rbtn_udp
                checked: true
                text: qsTr("UDP协议")
                onClicked :  {
                    txt_ioParams_1.text = qsTr("本机")
                    txt_ioParams_2.text = qsTr("对方")
                    txtIn_ioParams_1.text = qsTr("192.168.0.100 8080")
                    txtIn_ioParams_2.text = qsTr("192.168.0.99 8000")
                }
            }
            RadioButton {
                id : rbtn_uart
                text: qsTr("串口传输")
                onClicked: {
                    txt_ioParams_1.text = qsTr("串口端口号")
                    txt_ioParams_2.text = qsTr("波特率")
                    txtIn_ioParams_1.text = qsTr("COM1")
                    txtIn_ioParams_2.text = qsTr("115200")
                }
            }
            RadioButton {
                id : rbtn_can
                text: qsTr("CAN传输")
                onClicked: {
                    txt_ioParams_1.text = qsTr("CAN端口号")
                    txt_ioParams_2.text = qsTr("波特率")
                    txtIn_ioParams_1.text = qsTr("CAN1")
                    txtIn_ioParams_2.text = qsTr("1000K")
                }
            }
        }
    }

    Grid {
        id : set_param
        anchors.top : set_mode.bottom
        anchors.left : parent.left
        anchors.topMargin : 10
        anchors.leftMargin : 15
        layoutDirection : Grid.LeftToRight
        verticalItemAlignment: Grid.AlignVCenter
        spacing : 10
        rows : 2
        columns : 3
        Text {text: qsTr("目标文件:")}
        Rectangle {
            id : rect_filename
            width : 400
            height : 30
            border.color: "black"
            TextInput {
                id : txt_filename
                width : rect_filename.width
                wrapMode : TextInput.Wrap
                text : "目标文件地址"
                anchors.left : parent.left
                anchors.leftMargin : 10
                anchors.verticalCenter: parent.verticalCenter
            }
        }
        Button {
            id : btn_selectFile
            width : 100
            height : rect_filename.height
            text : "选择文件"
            onClicked : fileDialog.open()
        }
        Text {text : qsTr("网络设置:")}
        Grid {
            spacing : 10
            rows : 1
            columns : 2
            Rectangle{
                id : rect_ioParams_1
                width : rect_filename.width / 2 - parent.spacing / 2
                height : rect_filename.height
                border.color:  "black"
                TextInput {
                    id : txtIn_ioParams_1
                    text : "192.168.0.100 8080"
                    anchors.left : parent.left
                    anchors.leftMargin : 10
                    anchors.verticalCenter: parent.verticalCenter
                }
                Text {
                    id : txt_ioParams_1
                    text : "本机"
                    anchors.right : parent.right
                    anchors.rightMargin : 10
                    anchors.verticalCenter: parent.verticalCenter
                    color: "grey"
                }
            }
            Rectangle {
                id : rect_ioParams_2
                width : rect_ioParams_1.width
                height : rect_filename.height
                border.color:  "black"
                TextInput {
                    id : txtIn_ioParams_2
                    text : "192.168.0.99 8000"
                    anchors.left : parent.left
                    anchors.leftMargin : 10
                    anchors.verticalCenter: parent.verticalCenter
                }
                Text {
                    id : txt_ioParams_2
                    text : "对方"
                    anchors.right : parent.right
                    anchors.rightMargin : 10
                    anchors.verticalCenter: parent.verticalCenter
                    color: "grey"
                }
            }
        }
        Button {
            id : btn_connectIo
            width : btn_selectFile.width
            height : btn_selectFile.height
            text: qsTr("连接")
            onClicked: {
                backendSysnc()
                if (btn_connectIo.text === qsTr("连接")) {
                    Backend.sig_OpenIo();
                    btn_connectIo.text = qsTr("断开")
                } else {
                    Backend.sig_CloseIo();
                    btn_connectIo.text = qsTr("连接")
                }
            }
        }
    }

    Grid {
        id : set_task
        anchors.top : set_param.bottom
        anchors.left : parent.left
        anchors.topMargin : 10
        anchors.leftMargin : 15
        layoutDirection : Grid.LeftToRight
        verticalItemAlignment: Grid.AlignVCenter
        horizontalItemAlignment: Grid.AlignHCenter
        spacing : 10
        rows : 1
        columns : 3
        Button {
            id : btn_exec
            width : btn_selectFile.width
            height : btn_selectFile.height
            text: "执行"
            onClicked: {
                btn_exec.enabled = false
                backendSysnc()
                Backend.sig_Exec()
            }
        }
        Button {
            id : btn_md5
            width : btn_selectFile.width
            height : btn_selectFile.height
            text: "计算MD5"
            onClicked: {
                Backend.sig_Md5()
            }
        }
        TextInput {
            id : txt_result
            text : qsTr("结果显示")
        }
    }

    Rectangle {
        id : rect_showProc
        width : parent.width - 30
        height: parent.height - 210
        anchors.top : set_task.bottom
        anchors.left: parent.left
        anchors.topMargin : 10
        anchors.leftMargin : 15
        border.color : "black"

        ScrollView {
            id: view
            anchors.fill: rect_showProc
            TextArea {
                id : txt_showProc
                width : rect_showProc - 30
                wrapMode: TextInput.Wrap
                anchors.top : parent.top
                anchors.left : parent.left
                anchors.topMargin : 10
                anchors.leftMargin : 10
                background: Rectangle {border.color: "black"}
                placeholderText: qsTr("执行过程:")
            }
        }
    }

    FileDialog {
        id : fileDialog
        fileMode : FileDialog.OpenFile
        nameFilters : ["bin files (*.bin)", "any files(*.*)"]
        onAccepted : {
            txt_filename.text = selectedFile
            Backend.file = selectedFile
        }
    }

    Connections {
        target : Backend
        function onSig_UpdateResultText(str) {
            txt_result.text = str;
        }
    }

    Connections {
        target : Backend
        function onSig_UpdateProcText(str) {
            txt_showProc.text = txt_showProc.text + str;
            txt_showProc.cursorPosition = txt_showProc.length;
        }
    }

    Connections {
        target : Backend
        function onSig_ExecDone() {
            btn_exec.enabled = true
        }
    }

    function backendSysnc() {
        Backend.modeSend = rbtn_send.checked
        Backend.modeRecv = rbtn_recv.checked
        Backend.modeUdp  = rbtn_udp.checked
        Backend.modeUart = rbtn_uart.checked
        Backend.modeCan  = rbtn_can.checked
        Backend.file     = fileDialog.selectedFile
        Backend.ioParam1 = txtIn_ioParams_1.text
        Backend.ioParam2 = txtIn_ioParams_2.text
    }
}
